import { deleteRightTodo, deleteRightItem, spliceGetDataTodo, getSaleNum, getValueArray, setEmptyArray, setArrayValue, setExchangeValue, spliceGetDataItem } from '@/views/sales/common/TableSaleMethods'

export default ({
  state: {
    saleData: '',
    GetsaleData: '',
    giftData: [],
    GetgiftData1: [],
    GetgiftData2: [],
    GetgiftData3: [],
    form: {
      name: '',
      date: [],
      grade: [],
      gender: '1',
      term: 1,
      discount1: [{ 'value': '', 'n': '', isParcel: false }],
      discount2: [{ 'value': '', 'n': '' }],
      discountGift: [{
        'value': '',
        'n': '',
        isParcel: false
      }],
      remarks: ''
    }
  },
  getters: {
    getFullGiftSaleNum (state) {
      return getSaleNum(state.GetsaleData, {
        childName: 'productSkuList'
      })
    },
    getFullGiftSaleGift1Num (state) {
      return getSaleNum(state.GetgiftData1, {
        childName: 'productSkuList'
      })
    },
    getFullGiftSaleGift2Num (state) {
      return getSaleNum(state.GetgiftData2, {
        childName: 'productSkuList'
      })
    },
    getFullGiftSaleGift3Num (state) {
      return getSaleNum(state.GetgiftData3, {
        childName: 'productSkuList'
      })
    },
    ChoiceGifts2 (state) {
      if (state.form.term == 1) {
        if (!(state.form.discountGift[0])) {
          return false
        } else {
          return `满${state.form.discountGift[0].value}元赠品区`
        }
      } else if (state.form.term == 2) {
        if (!(state.form.discount1[0])) {
          return false
        } else {
          return `满${state.form.discount1[0].value}元赠品区`
        }
      }
    },
    ChoiceGifts3 (state) {
      if (state.form.term == 1) {
        if (!(state.form.discountGift[1])) {
          return false
        } else {
          return `满${state.form.discountGift[1].value}元赠品区`
        }
      } else if (state.form.term == 2) {
        if (!(state.form.discount1[1])) {
          return false
        } else {
          return `满${state.form.discount1[1].value}元赠品区`
        }
      }
    },
    ChoiceGifts4 (state) {
      if (state.form.term == 1) {
        if (!(state.form.discountGift[2])) {
          return false
        } else {
          return `满${state.form.discountGift[2].value}元赠品区`
        }
      } else if (state.form.term == 2) {
        if (!(state.form.discount1[2])) {
          return false
        } else {
          return `满${state.form.discount1[2].value}元赠品区`
        }
      }
    },
    hasPatchAdd (state) {
      if ((state.form.discount1.length == 1 && state.form.discount1[0].value) || (state.form.discountGift.length == 1 && state.form.discountGift[0].value)) {
        return true
      } else {
        return false
      }
    },
    hasGift1 (state) {
      let word = false
      if (state.form.discount1[0]) {
        word = true
      } else if (state.form.discountGift[0] && state.form.discountGift[1]) {
        word = true
      } else {
        word = false
      }
      return word
    },
    hasGift2 (state) {
      let word = false
      if (state.form.discount1[1]) {
        word = true
      } else if (state.form.discountGift[1]) {
        word = true
      } else {
        word = false
      }
      return word
    },
    hasGift3 (state) {
      let word = false
      if (state.form.discount1[2]) {
        word = true
      } else if (state.form.discountGift[2]) {
        word = true
      } else {
        word = false
      }
      return word
    },
    fullGiftSelecterm (state) {
      let word
      if (state.form.term == 1) {
        word = '满赠促销'
      } else {
        word = '加价购促销'
      }
      return word
    },
    fullGiftSelectgrade (state, getters) {
      let newarray = []
      for (let item of state.form.grade) {
        for (let todo of getters.storeTypeData) {
          if (item == todo.value) {
            newarray.push(todo.text)
          }
        }
      }
      return newarray.join(',')
    }
  },
  mutations: {
    setFullGiftSaleEmpty (state) {
      state.saleData = ''
      state.GetsaleData = ''
      state.giftData = ''
      state.GetgiftData1 = []
      state.GetgiftData2 = []
      state.GetgiftData3 = []
      state.form = {
        name: '',
        date: [],
        grade: [],
        gender: '1',
        term: 1,
        discount1: [{ 'value': '', 'n': '', isParcel: false }],
        discount2: [{ 'value': '', 'n': '' }],
        discountGift: [{
          'value': '',
          'n': '',
          isParcel: false
        }],
        remarks: ''
      }
    },
    getFullGiftData (state, type) {
      let getdata = JSON.parse(JSON.stringify(state.saleData))
      // state.GetsaleData = JSON.parse(JSON.stringify(state.saleData));
      getValueArray(getdata, {
        index: type,
        childName: 'productSkuList',
        status: false
      })
      setEmptyArray(getdata, {
        index: type,
        childName: 'productSkuList'
      })
      setArrayValue(getdata, {
        index: type,
        val1: false,
        childName: 'productSkuList'
      })
      if (!state.GetsaleData.length) { //首次为空
        state.GetsaleData = getdata
      } else { // 防止每次更新的淹没其他的
        //把没有的第一层数据存在，GetsaleData里面
        //把没有的第二层的数据存在, GetsaleData里面
        spliceGetDataItem(getdata, state.GetsaleData)
        spliceGetDataTodo(getdata, state.GetsaleData)
      }
      if (type !== 'buttonChecked') {
        setExchangeValue(state.saleData, { index1: 'checked', index: 'buttonChecked', childName: 'productSkuList' })
      }
    },
    deleteFullGiftData (state, object) {
      if (object.todo && !object.todo.buttonChecked || object.item && !object.item.buttonChecked) {
        if (object.todo) { //没子集
          deleteRightTodo(object.item, object.todo, state.GetsaleData)
        } else { //有子集
          deleteRightItem(object.item, state.GetsaleData)
        }
      }
    },
    GetfgsgiftData1 (state) {
      let getdata = JSON.parse(JSON.stringify(state.giftData))
      // state.GetgiftData1 = JSON.parse(JSON.stringify(state.giftData));
      getValueArray(getdata, {
        index: 'checked',
        childName: 'productSkuList',
        status: false
      })
      setEmptyArray(getdata, {
        index: 'checked',
        childName: 'productSkuList'
      })
      setArrayValue(getdata, {
        index: 'checked',
        val1: false,
        childName: 'productSkuList'
      })
      if (!state.GetgiftData1.length) { //首次为空
        state.GetgiftData1 = getdata
      } else { // 防止每次更新的淹没其他的
        //把没有的第一层数据存在，GetgiftData里面
        //把没有的第二层的数据存在, GetgiftData里面
        spliceGetDataItem(getdata, state.GetgiftData1)
        spliceGetDataTodo(getdata, state.GetgiftData1)
      }
    },
    GetfgsgiftData2 (state) {
      let getdata = JSON.parse(JSON.stringify(state.giftData))
      // state.GetgiftData1 = JSON.parse(JSON.stringify(state.giftData));
      getValueArray(getdata, {
        index: 'checked',
        childName: 'productSkuList',
        status: false
      })
      setEmptyArray(getdata, {
        index: 'checked',
        childName: 'productSkuList'
      })
      setArrayValue(getdata, {
        index: 'checked',
        val1: false,
        childName: 'productSkuList'
      })
      if (!state.GetgiftData2.length) { //首次为空
        state.GetgiftData2 = getdata
      } else { // 防止每次更新的淹没其他的
        //把没有的第一层数据存在，GetgiftData里面
        //把没有的第二层的数据存在, GetgiftData里面
        spliceGetDataItem(getdata, state.GetgiftData2)
        spliceGetDataTodo(getdata, state.GetgiftData2)
      }
    },
    GetfgsgiftData3 (state) {
      let getdata = JSON.parse(JSON.stringify(state.giftData))
      // state.GetgiftData1 = JSON.parse(JSON.stringify(state.giftData));
      getValueArray(getdata, {
        index: 'checked',
        childName: 'productSkuList',
        status: false
      })
      setEmptyArray(getdata, {
        index: 'checked',
        childName: 'productSkuList'
      })
      setArrayValue(getdata, {
        index: 'checked',
        val1: false,
        childName: 'productSkuList'
      })
      if (!state.GetgiftData3.length) { //首次为空
        state.GetgiftData3 = getdata
      } else { // 防止每次更新的淹没其他的
        //把没有的第一层数据存在，GetgiftData里面
        //把没有的第二层的数据存在, GetgiftData里面
        spliceGetDataItem(getdata, state.GetgiftData3)
        spliceGetDataTodo(getdata, state.GetgiftData3)
      }
    },
    onReduceGiftDiscount (state, index) {
      state.form.discountGift.splice(index, 1)
    },
    onAddGiftDiscount (state) {
      if (state.form.discountGift.length <= 2) {
        state.form.discountGift.push({
          'value': '',
          'n': '',
          isParcel: false
        })
      }
    },
    onfgsAddDiscount (state) {
      if (state.form.discount1.length <= 2) {
        state.form.discount1.push({
          'value': '',
          'n': '',
          isParcel: false
        })
        state.form.discount2.push({
          'value': '',
          'n': ''
        })
      }
    },
    onfgsReduceDiscount (state, index) {
      state.form.discount1.splice(index, 1)
      state.form.discount2.splice(index, 1)
    },
    getSaleData (state) {
      state.GetsaleData = JSON.parse(JSON.stringify(state.saleData))
    }
  }
})
